--[[
CastYeller2 - Announce important spell events.

Copyright (C) 2009 Adirelle

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--]]

if not CastYeller2 then return end
local CastYeller2 = CastYeller2 
local L = CastYeller2.L

local mod, super = CastYeller2:NewSourceModule('Auras')

mod.moduleTitle = L['Auras']

mod.triggers = {
	buffApplied = L['Buff gained'],
	buffRemoved = L['Buff lost'],
	debuffApplied = L['Debuff gained'],
	debuffRemoved = L['Debuff lost'],
	debuffBroken = L['Debuff broken'],
}

mod.defaultAnnounces = {
	buffApplied = L['$TARGET[ ${RAIDICONTEXTURE}] gained $SPELL[ (${STACK})]'],
	buffRemoved = L['$SPELL faded from $TARGET[ ${RAIDICONTEXTURE}][ (${STACK})]'],
	debuffApplied = L['$TARGET[ ${RAIDICONTEXTURE}] suffers from $SPELL[ (${STACK})]'],
	debuffRemoved = L['$SPELL faded from $TARGET[ ${RAIDICONTEXTURE}][ (${STACK})]'],
	debuffBroken = L['$SPELL on $TARGET[ ${RAIDICONTEXTURE}] has been broken'],
}

local pName = UnitName('player')

function mod:OnEnable()
	super.OnEnable(self)
	self:RegisterCombatListener('SPELL_AURA_APPLIED')
	self:RegisterCombatListener('SPELL_AURA_REMOVED')
	self:RegisterCombatListener('SPELL_AURA_BROKEN')
	self:RegisterCombatListener('SPELL_AURA_BROKEN_SPELL')
	self:RegisterCombatListener('SPELL_AURA_APPLIED_DOSE')
	self:RegisterCombatListener('SPELL_AURA_REMOVED_DOSE')
end

function mod:SPELL_AURA_APPLIED(_, _, _, source, _, targetGUID, target, targetFlags, spellId, spell, _, buffType)
	if buffType == 'BUFF' and (target == pName or source == pName) then
		self:TriggerAnnounce('buffApplied', target, spell, nil, spellId, targetGUID, targetFlags)
	elseif buffType == 'DEBUFF' and source == pName then
		self:TriggerAnnounce('debuffApplied', target, spell, nil, spellId, targetGUID, targetFlags)
	end
end

function mod:SPELL_AURA_APPLIED_DOSE(_, _, _, source, _, targetGUID, target, targetFlags, spellId, spell, _, buffType, amount)
	if buffType == 'BUFF' and (target == pName or source == pName) then
		self:TriggerAnnounce('buffApplied', target, spell, nil, spellId, targetGUID, targetFlags, 'stack', amount)
	elseif buffType == 'DEBUFF' and source == pName then
		self:TriggerAnnounce('debuffApplied', target, spell, nil, spellId, targetGUID, targetFlags, 'stack', amount)
	end
end

function mod:SPELL_AURA_REMOVED(_, _, _, source, _, targetGUID, target, targetFlags, spellId, spell, _, buffType)
	if buffType == 'BUFF' and (target == pName or source == pName) then
		self:TriggerAnnounce('buffRemoved', target, spell, nil, spellId, targetGUID, targetFlags)
	elseif buffType == 'DEBUFF' and source == pName then
		self:TriggerAnnounce('debuffRemoved', target, spell, nil, spellId, targetGUID, targetFlags)
	end	
end

function mod:SPELL_AURA_REMOVED_DOSE(_, _, _, source, _, targetGUID, target, targetFlags, spellId, spell, _, buffType, amount)
	if buffType == 'BUFF' and (target == pName or source == pName) then
		self:TriggerAnnounce('buffRemoved', target, spell, nil, spellId, targetGUID, targetFlags, 'stack', amount)
	elseif buffType == 'DEBUFF' and source == pName then
		self:TriggerAnnounce('debuffRemoved', target, spell, nil, spellId, targetGUID, targetFlags, 'stack', amount)
	end	
end

function mod:SPELL_AURA_BROKEN(_, _, _, source, _, targetGUID, target, targetFlags, spellId, spell, _, buffType)
	if buffType == 'DEBUFF' and source == pName then
		self:TriggerAnnounce('debuffBroken', target, spell, nil, spellId, targetGUID, targetFlags)
	end	
end

function mod:SPELL_AURA_BROKEN_SPELL(_, _, _, source, _, targetGUID, target, targetFlags, spellId, spell, _, _, _, _, buffType)
	if buffType == 'DEBUFF' and source == pName then
		self:TriggerAnnounce('debuffBroken', target, spell, nil, spellId, targetGUID, targetFlags)
	end	
end
